package ar.edu.itba.it.pdc.app.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class Client {

	private Socket socket;
	private String username;
	private boolean isLogged;
	
	public Client(Socket socket){
		this.socket = socket;
		this.username = null;
		this.isLogged = false;
	}

	public Socket getSocket(){
		return socket;
	}
	
	public String getUsername(){
		return username;
	}
	
	public void setUsername(String username){
		this.username = username;
	}
	
	public boolean isLogged(){
		return isLogged;
	}
	
	public void login(){
		this.isLogged = true;
	}
	
	public String getIP() {
		return socket.getInetAddress().getHostAddress();
	}
	
	public String getHost(){
		return socket.getInetAddress().getHostName();
	}
	
	public int getPort(){
		return socket.getPort();
	}
	
	public InputStream getInputStream() throws IOException {
		return socket.getInputStream();
	}
	
	public OutputStream getOutputStream() throws IOException {
		return socket.getOutputStream();
	}

	public InetAddress getRemoteIP() {
		return socket.getInetAddress();
	}

	public void close() throws IOException {
		socket.close();
	}

	public boolean isClosed() {
		return socket.isClosed();
	}
	
	public void warn(String warning) throws IOException {
		getOutputStream().write(warning.getBytes());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((socket == null) ? 0 : socket.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (socket == null) {
			if (other.socket != null)
				return false;
		} else if (!socket.equals(other.socket))
			return false;
		return true;
	}
}
